#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int res = 0;
    int search(TreeNode* root, int k, int target)
    {
        if (root == nullptr) return k;
        k = search(root->left, k, target);
        if (k == target) res = root->val;
        int ret = search(root->right, k + 1, target);
        return ret;
    }
    int kthSmallest(TreeNode* root, int k) {
        search(root, 1, k);
        return res;
    }
};